package keeto.com.vn.classified.adapter;

import java.util.ArrayList;

import keeto.com.vn.classified.network.entity.ListItemEntity;
import keeto.com.vn.classified.ui.IADetailProductActivity;
import keeto.com.vn.classified.ui.R;
import keeto.com.vn.classified.utils.StringUtilities;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.androidquery.AQuery;

public class SubProductAdapter extends BaseAdapter {
	private Context context = null;
	private LayoutInflater mInflater;
	private ArrayList<ListItemEntity> mArrayList;
	private AQuery mQuery;
	
	
	public SubProductAdapter(Context context,ArrayList<ListItemEntity> mArrayList, AQuery mQuery) {
		super();
		this.context = context;
		this.mArrayList = mArrayList;
		this.mInflater = LayoutInflater.from(context);
		this.mQuery = mQuery;
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView != null && convertView.getId() == R.layout.ia_product_list_view_item_new) {
			holder = (ViewHolder) convertView.getTag();
		}else{
			convertView = mInflater.inflate(R.layout.ia_product_list_view_item_new,parent, false);
			convertView.setId(R.layout.ia_product_list_view_item_new);
			holder = new ViewHolder();
			holder.image_product = (ImageView)convertView.findViewById(R.id.image_product);
			holder.tv_discount = (TextView)convertView.findViewById(R.id.tv_discount);
			holder.tv_tittle = (TextView)convertView.findViewById(R.id.tv_title);
			holder.tv_description = (TextView)convertView.findViewById(R.id.tv_description);
			holder.tv_price = (TextView)convertView.findViewById(R.id.tv_price);
			holder.ratingbar_product = (RatingBar)convertView.findViewById(R.id.ratingbar_product);
			holder.tv_discount.setVisibility(View.GONE);
			convertView.setTag(holder);
		}
		holder.position = position;
		final ListItemEntity product = mArrayList.get(position);
		if (product.getCoverPicture().size() > 0 && !TextUtils.isEmpty(product.getCoverPicture().get(0))) {

			String url = product
					.getCoverPicture().get(0);
			mQuery.id(holder.image_product).image(url, true, true, 0, 0, null, 0, 1.0f);

		} else {
			mQuery.id(holder.image_product).image(R.drawable.no_image);

		}

		/*
		 * set data to UI
		 */
		holder.tv_tittle.setText(product.getTitle());

		if(product.getSubDec() != null && product.getSubDec().length() > 0){
			holder.tv_description.setText(Html.fromHtml(product.getSubDec()));
		}else{
			holder.tv_description.setText("");
		}

		/*
		 * price
		 */
		String price = StringUtilities.convertPrice(product.getPrice());
		if (product.getLisPromotionHomeEntities() != null){
			String discount = product.getLisPromotionHomeEntities().getDiscount();
			if (discount != null && !discount.equals("0")){
				holder.tv_discount.setText(Integer.parseInt("-" + discount.trim()) +"%");
				holder.tv_discount.setVisibility(View.VISIBLE);
				String new_price = StringUtilities.convertNewPrice(product.getPrice(), Integer.parseInt(discount.trim()));
				if (!TextUtils.isEmpty(new_price) && !new_price.equals("0")) {
					holder.tv_price.setText(new_price + " VNĐ");
					holder.tv_price.setVisibility(View.VISIBLE);
				} else {
					holder.tv_price.setVisibility(View.INVISIBLE);
				}
			}else{
				holder.tv_discount.setVisibility(View.GONE);
				if (!TextUtils.isEmpty(price) && !price.equals("0")) {
					holder.tv_price.setVisibility(View.VISIBLE);
					holder.tv_price.setText(price + " VNĐ");
				} else {
					String additionPrice = product.getAdditionPrice();
					if(additionPrice != null && additionPrice.length() > 0){
						holder.tv_price.setText(Html.fromHtml(additionPrice));
						holder.tv_price.setVisibility(View.VISIBLE);
					}else{
						holder.tv_price.setVisibility(View.INVISIBLE);
					}
				}
			}
		}else {
			holder.tv_discount.setVisibility(View.GONE);
			if (!TextUtils.isEmpty(price) && !price.equals("0")) {
				holder.tv_price.setVisibility(View.VISIBLE);
				holder.tv_price.setText(price + " VNĐ");
			} else {
				String additionPrice = product.getAdditionPrice();
				if(additionPrice != null && additionPrice.length() > 0){
					holder.tv_price.setText(Html.fromHtml(additionPrice));
					holder.tv_price.setVisibility(View.VISIBLE);
				}else{
					holder.tv_price.setVisibility(View.INVISIBLE);
				}
			}
		}

		/*
		 * ratingbar:
		 */
		float rating = product.getRating();
		holder.ratingbar_product.setRating(rating);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
					IADetailProductActivity.actionLaunchProduct(context, product);
				
			}
		});
		return convertView;
	}
	public class ViewHolder {
		private ImageView image_product;
		private TextView tv_discount;
		private TextView tv_tittle;
		private TextView tv_description;
		private TextView tv_price;
		private RatingBar ratingbar_product;
		private int position = -1;

	}
}
